/*
* @Description: useCategory
* @Author: yxm
* @Date: 2023-11-21
*/


import {onBeforeRouteUpdate, useRoute} from "vue-router";
import {getCategoryAPI} from "@/apis/category";
import {onMounted, ref} from "vue";

export function useCategory(){
    
    const categoryData = ref({})
    const route = useRoute()
//括号内指 传参和默认参数 的区别
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)
        categoryData.value = res.result
    }

// // beforeupdate可以在每次更新路由之前执行
// onBeforeUpdate( () => getCategory())
    
    onMounted( () => getCategory())
    
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)
    })
    return {
        categoryData
    }
}